
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>头像接入 · Passport业务手册</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="xiajie <xiajie01@baidu.com>">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-multipart/multipart.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-filename/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-page-footer-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-okaidia.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-custom-js-css/03c70a23567f2990c05f5cb8c64400a2-main.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-custom-js-css/27b4a027a84f513ce71206e15df835dc-folding-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="../auth-widget/code.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
    

    

    
        
        <li class="header">[第一章] 开发指南</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                        <b>1.1.</b>
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../guides/develop/awareness.html">
            
                <a href="../../guides/develop/awareness.html">
            
                    
                        <b>1.2.</b>
                    
                    新人必读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../guides/develop/doc.html">
            
                <a href="../../guides/develop/doc.html">
            
                    
                        <b>1.3.</b>
                    
                    文档撰写
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../guides/services.html">
            
                <a href="../../guides/services.html">
            
                    
                        <b>1.4.</b>
                    
                    流量接入介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../guides/services/shifen.html">
            
                <a href="../../guides/services/shifen.html">
            
                    
                        <b>1.4.1.</b>
                    
                    灵渠(shifen平台)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../guides/services/bgw.html">
            
                <a href="../../guides/services/bgw.html">
            
                    
                        <b>1.4.2.</b>
                    
                    BGW
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../guides/services/bfe.html">
            
                <a href="../../guides/services/bfe.html">
            
                    
                        <b>1.4.3.</b>
                    
                    BFE
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../guides/passport.html">
            
                <a href="../../guides/passport.html">
            
                    
                        <b>1.5.</b>
                    
                    Pass技术架构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../guides/passport/dbgate.html">
            
                <a href="../../guides/passport/dbgate.html">
            
                    
                        <b>1.5.1.</b>
                    
                    DBGATE
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../guides/passport/session.html">
            
                <a href="../../guides/passport/session.html">
            
                    
                        <b>1.5.2.</b>
                    
                    SESSION
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                        <b>1.6.</b>
                    
                    开发环境配置
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../guides/environment/instances.html">
            
                <a href="../../guides/environment/instances.html">
            
                    
                        <b>1.6.1.</b>
                    
                    开发机实例创建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../guides/environment/server_tool.html">
            
                <a href="../../guides/environment/server_tool.html">
            
                    
                        <b>1.6.2.</b>
                    
                    开发机配套工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../../guides/environment/tool.html">
            
                <a href="../../guides/environment/tool.html">
            
                    
                        <b>1.6.3.</b>
                    
                    本地常用工具
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">[第二章] 业务项目</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../projects/experiment/">
            
                <a href="../../projects/experiment/">
            
                    
                        <b>2.1.</b>
                    
                    分层实验平台
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../../projects/experiment/concept.html">
            
                <a href="../../projects/experiment/concept.html">
            
                    
                        <b>2.1.1.</b>
                    
                    基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../../projects/experiment/concept_parse.html">
            
                <a href="../../projects/experiment/concept_parse.html">
            
                    
                        <b>2.1.2.</b>
                    
                    概念剖析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../projects/experiment/quick_start.html">
            
                <a href="../../projects/experiment/quick_start.html">
            
                    
                        <b>2.1.3.</b>
                    
                    快速开始
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.3.1" data-path="../../projects/experiment/quick_start_experiment.html">
            
                <a href="../../projects/experiment/quick_start_experiment.html">
            
                    
                        <b>2.1.3.1.</b>
                    
                    实验创建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.2" data-path="../../projects/experiment/quick_start_usage.html">
            
                <a href="../../projects/experiment/quick_start_usage.html">
            
                    
                        <b>2.1.3.2.</b>
                    
                    业务层应用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.3" data-path="../../projects/experiment/quick_start_chart.html">
            
                <a href="../../projects/experiment/quick_start_chart.html">
            
                    
                        <b>2.1.3.3.</b>
                    
                    报表查询
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.4" >
            
                <span>
            
                    
                        <b>2.1.4.</b>
                    
                    实现机制
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.4.1" data-path="../../projects/experiment/implement_platform.html">
            
                <a href="../../projects/experiment/implement_platform.html">
            
                    
                        <b>2.1.4.1.</b>
                    
                    实验平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4.2" data-path="../../projects/experiment/implement_process.html">
            
                <a href="../../projects/experiment/implement_process.html">
            
                    
                        <b>2.1.4.2.</b>
                    
                    实验流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4.3" data-path="../../projects/experiment/implement_sdk.html">
            
                <a href="../../projects/experiment/implement_sdk.html">
            
                    
                        <b>2.1.4.3.</b>
                    
                    SDK
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4.4" data-path="../../projects/experiment/implement_expression.html">
            
                <a href="../../projects/experiment/implement_expression.html">
            
                    
                        <b>2.1.4.4.</b>
                    
                    表达式执行器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4.5" data-path="../../projects/experiment/implement_chart.html">
            
                <a href="../../projects/experiment/implement_chart.html">
            
                    
                        <b>2.1.4.5.</b>
                    
                    通用报表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../../projects/experiment/cases.html">
            
                <a href="../../projects/experiment/cases.html">
            
                    
                        <b>2.1.5.</b>
                    
                    应用案例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../../projects/experiment/reference.html">
            
                <a href="../../projects/experiment/reference.html">
            
                    
                        <b>2.1.6.</b>
                    
                    参考资料
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../projects/auth-widget/">
            
                <a href="../../projects/auth-widget/">
            
                    
                        <b>2.2.</b>
                    
                    验证控件重构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" >
            
                <span>
            
                    
                        <b>2.2.1.</b>
                    
                    当前现状
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1.1" data-path="../../projects/auth-widget/current/authtoken.html">
            
                <a href="../../projects/auth-widget/current/authtoken.html">
            
                    
                        <b>2.2.1.1.</b>
                    
                    初始化阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.1.2" data-path="../../projects/auth-widget/current/render.html">
            
                <a href="../../projects/auth-widget/current/render.html">
            
                    
                        <b>2.2.1.2.</b>
                    
                    渲染阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.1.3" data-path="../../projects/auth-widget/current/check.html">
            
                <a href="../../projects/auth-widget/current/check.html">
            
                    
                        <b>2.2.1.3.</b>
                    
                    验证阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.1.4" data-path="../../projects/auth-widget/current/use.html">
            
                <a href="../../projects/auth-widget/current/use.html">
            
                    
                        <b>2.2.1.4.</b>
                    
                    使用阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.1.5" data-path="../../projects/auth-widget/current/now.html">
            
                <a href="../../projects/auth-widget/current/now.html">
            
                    
                        <b>2.2.1.5.</b>
                    
                    使用场景
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="../../projects/auth-widget/refactor.html">
            
                <a href="../../projects/auth-widget/refactor.html">
            
                    
                        <b>2.2.2.</b>
                    
                    重构实现
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.2.1" data-path="../../projects/auth-widget/refactor/context.html">
            
                <a href="../../projects/auth-widget/refactor/context.html">
            
                    
                        <b>2.2.2.1.</b>
                    
                    验证控件上下文
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2.2" data-path="../../projects/auth-widget/refactor/interface.html">
            
                <a href="../../projects/auth-widget/refactor/interface.html">
            
                    
                        <b>2.2.2.2.</b>
                    
                    验证控件抽象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2.3" data-path="../../projects/auth-widget/refactor/mobile.html">
            
                <a href="../../projects/auth-widget/refactor/mobile.html">
            
                    
                        <b>2.2.2.3.</b>
                    
                    手机验证
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2.4" data-path="../../projects/auth-widget/refactor/email.html">
            
                <a href="../../projects/auth-widget/refactor/email.html">
            
                    
                        <b>2.2.2.4.</b>
                    
                    邮箱验证
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2.5" data-path="../../projects/auth-widget/refactor/facematch.html">
            
                <a href="../../projects/auth-widget/refactor/facematch.html">
            
                    
                        <b>2.2.2.5.</b>
                    
                    刷脸验证
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2.3" >
            
                <span>
            
                    
                        <b>2.2.3.</b>
                    
                    新版使用
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.3.1" data-path="../../projects/auth-widget/new/authtoken.html">
            
                <a href="../../projects/auth-widget/new/authtoken.html">
            
                    
                        <b>2.2.3.1.</b>
                    
                    初始化阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3.2" data-path="../../projects/auth-widget/new/render.html">
            
                <a href="../../projects/auth-widget/new/render.html">
            
                    
                        <b>2.2.3.2.</b>
                    
                    渲染阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3.3" data-path="../../projects/auth-widget/new/check.html">
            
                <a href="../../projects/auth-widget/new/check.html">
            
                    
                        <b>2.2.3.3.</b>
                    
                    验证阶段
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3.4" data-path="../../projects/auth-widget/new/use.html">
            
                <a href="../../projects/auth-widget/new/use.html">
            
                    
                        <b>2.2.3.4.</b>
                    
                    使用阶段
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../../projects/mobile-refactor/">
            
                <a href="../../projects/mobile-refactor/">
            
                    
                        <b>2.3.</b>
                    
                    联系手机
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../../projects/mobile-refactor/ppui.html">
            
                <a href="../../projects/mobile-refactor/ppui.html">
            
                    
                        <b>2.3.1.</b>
                    
                    业务层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../../projects/mobile-refactor/dbgate.html">
            
                <a href="../../projects/mobile-refactor/dbgate.html">
            
                    
                        <b>2.3.2.</b>
                    
                    后端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="../../projects/mobile-refactor/plan.html">
            
                <a href="../../projects/mobile-refactor/plan.html">
            
                    
                        <b>2.3.3.</b>
                    
                    开发方案
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../../projects/portrait-to-orp/orp/">
            
                <a href="../../projects/portrait-to-orp/orp/">
            
                    
                        <b>2.4.</b>
                    
                    头像迁移ORP
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../../projects/portrait-to-orp/orp/evaluate.html">
            
                <a href="../../projects/portrait-to-orp/orp/evaluate.html">
            
                    
                        <b>2.4.1.</b>
                    
                    流量评估
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../../projects/portrait-to-orp/orp/deploy.html">
            
                <a href="../../projects/portrait-to-orp/orp/deploy.html">
            
                    
                        <b>2.4.2.</b>
                    
                    服务部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../../projects/portrait-to-orp/orp/test.html">
            
                <a href="../../projects/portrait-to-orp/orp/test.html">
            
                    
                        <b>2.4.3.</b>
                    
                    测试方案
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="../../projects/portrait-to-orp/orp/test-result.html">
            
                <a href="../../projects/portrait-to-orp/orp/test-result.html">
            
                    
                        <b>2.4.4.</b>
                    
                    测试结论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.5" data-path="../../projects/portrait-to-orp/orp/bug.html">
            
                <a href="../../projects/portrait-to-orp/orp/bug.html">
            
                    
                        <b>2.4.5.</b>
                    
                    BUG收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.6" data-path="../../projects/portrait-to-orp/orp/service.html">
            
                <a href="../../projects/portrait-to-orp/orp/service.html">
            
                    
                        <b>2.4.6.</b>
                    
                    依赖服务梳理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.7" data-path="../../projects/portrait-to-orp/orp/monitor.html">
            
                <a href="../../projects/portrait-to-orp/orp/monitor.html">
            
                    
                        <b>2.4.7.</b>
                    
                    监控规则梳理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.8" data-path="../../projects/portrait-to-orp/orp/plan.html">
            
                <a href="../../projects/portrait-to-orp/orp/plan.html">
            
                    
                        <b>2.4.8.</b>
                    
                    计划排期
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../../projects/experiment/trace/">
            
                <a href="../../projects/experiment/trace/">
            
                    
                        <b>2.5.</b>
                    
                    TraceID机制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../../projects/experiment/trace/concept.html">
            
                <a href="../../projects/experiment/trace/concept.html">
            
                    
                        <b>2.5.1.</b>
                    
                    基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2" >
            
                <span>
            
                    
                        <b>2.5.2.</b>
                    
                    实现机制
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.2.1" data-path="../../projects/experiment/trace/trace_id.html">
            
                <a href="../../projects/experiment/trace/trace_id.html">
            
                    
                        <b>2.5.2.1.</b>
                    
                    TraceID及管理器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2.2" data-path="../../projects/experiment/trace/flow_id.html">
            
                <a href="../../projects/experiment/trace/flow_id.html">
            
                    
                        <b>2.5.2.2.</b>
                    
                    流程定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2.3" data-path="../../projects/experiment/trace/flow_handle.html">
            
                <a href="../../projects/experiment/trace/flow_handle.html">
            
                    
                        <b>2.5.2.3.</b>
                    
                    流程处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2.4" data-path="../../projects/experiment/trace/exp_append_trace_id.html">
            
                <a href="../../projects/experiment/trace/exp_append_trace_id.html">
            
                    
                        <b>2.5.2.4.</b>
                    
                    小流量实验
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2.5" data-path="../../projects/experiment/trace/trace_id_transfer.html">
            
                <a href="../../projects/experiment/trace/trace_id_transfer.html">
            
                    
                        <b>2.5.2.5.</b>
                    
                    TraceID传递
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.2.5.1" data-path="../../projects/experiment/trace/trace_id_transfer_ppui.html">
            
                <a href="../../projects/experiment/trace/trace_id_transfer_ppui.html">
            
                    
                        <b>2.5.2.5.1.</b>
                    
                    PPUI
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2.5.2" data-path="../../projects/experiment/trace/trace_id_transfer_fe.html">
            
                <a href="../../projects/experiment/trace/trace_id_transfer_fe.html">
            
                    
                        <b>2.5.2.5.2.</b>
                    
                    FE
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2.5.3" data-path="../../projects/experiment/trace/trace_id_transfer_na.html">
            
                <a href="../../projects/experiment/trace/trace_id_transfer_na.html">
            
                    
                        <b>2.5.2.5.3.</b>
                    
                    NA端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5.2.6" data-path="../../projects/experiment/trace/log.html">
            
                <a href="../../projects/experiment/trace/log.html">
            
                    
                        <b>2.5.2.6.</b>
                    
                    日志记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../../projects/face-cluster/">
            
                <a href="../../projects/face-cluster/">
            
                    
                        <b>2.6.</b>
                    
                    人脸聚类
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" >
            
                <span>
            
                    
                        <b>2.6.1.</b>
                    
                    存量聚类
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1.1" data-path="../../projects/face-cluster/clustering/feature.html">
            
                <a href="../../projects/face-cluster/clustering/feature.html">
            
                    
                        <b>2.6.1.1.</b>
                    
                    特征提取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.1.2" data-path="../../projects/face-cluster/clustering/cluster.html">
            
                <a href="../../projects/face-cluster/clustering/cluster.html">
            
                    
                        <b>2.6.1.2.</b>
                    
                    聚类输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="../../projects/face-cluster/face.html">
            
                <a href="../../projects/face-cluster/face.html">
            
                    
                        <b>2.6.2.</b>
                    
                    人脸管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.2.1" data-path="../../projects/face-cluster/face/register.html">
            
                <a href="../../projects/face-cluster/face/register.html">
            
                    
                        <b>2.6.2.1.</b>
                    
                    人脸注册
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.2.2" data-path="../../projects/face-cluster/face/search.html">
            
                <a href="../../projects/face-cluster/face/search.html">
            
                    
                        <b>2.6.2.2.</b>
                    
                    人脸搜索
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6.3" >
            
                <span>
            
                    
                        <b>2.6.3.</b>
                    
                    业务改造
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.3.1" data-path="../../projects/face-cluster/refactor/face.html">
            
                <a href="../../projects/face-cluster/refactor/face.html">
            
                    
                        <b>2.6.3.1.</b>
                    
                    刷脸开关
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.3.2" data-path="../../projects/face-cluster/refactor/face_login.html">
            
                <a href="../../projects/face-cluster/refactor/face_login.html">
            
                    
                        <b>2.6.3.2.</b>
                    
                    刷脸登录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6.4" data-path="../../projects/face-cluster/question.html">
            
                <a href="../../projects/face-cluster/question.html">
            
                    
                        <b>2.6.4.</b>
                    
                    问题探讨
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../../projects/face-platform/">
            
                <a href="../../projects/face-platform/">
            
                    
                        <b>2.7.</b>
                    
                    Pass人脸库建设
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.7.1" data-path="../../projects/face-platform/phase-1.html">
            
                <a href="../../projects/face-platform/phase-1.html">
            
                    
                        <b>2.7.1.</b>
                    
                    一期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.2" data-path="../../projects/face-platform/phase-2.html">
            
                <a href="../../projects/face-platform/phase-2.html">
            
                    
                        <b>2.7.2.</b>
                    
                    二期
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../../projects/ipv6/">
            
                <a href="../../projects/ipv6/">
            
                    
                        <b>2.8.</b>
                    
                    IPV6兼容改造
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" >
            
                <span>
            
                    
                        <b>2.9.</b>
                    
                    NA登录重构
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.9.1" data-path="../../projects/nalogin-refactor/">
            
                <a href="../../projects/nalogin-refactor/">
            
                    
                        <b>2.9.1.</b>
                    
                    需求
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9.2" data-path="../../projects/nalogin-refactor/experiment.html">
            
                <a href="../../projects/nalogin-refactor/experiment.html">
            
                    
                        <b>2.9.2.</b>
                    
                    新版页面小流量方案
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9.3" >
            
                <span>
            
                    
                        <b>2.9.3.</b>
                    
                    接口改造
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.9.3.1" data-path="../../projects/nalogin-refactor/refactor/check.html">
            
                <a href="../../projects/nalogin-refactor/refactor/check.html">
            
                    
                        <b>2.9.3.1.</b>
                    
                    帐号检测
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9.3.2" data-path="../../projects/nalogin-refactor/refactor/history.html">
            
                <a href="../../projects/nalogin-refactor/refactor/history.html">
            
                    
                        <b>2.9.3.2.</b>
                    
                    获取登录历史
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9.3.3" data-path="../../projects/nalogin-refactor/refactor/sendSMS.html">
            
                <a href="../../projects/nalogin-refactor/refactor/sendSMS.html">
            
                    
                        <b>2.9.3.3.</b>
                    
                    发送短信验证码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9.3.4" data-path="../../projects/nalogin-refactor/refactor/smsLogin.html">
            
                <a href="../../projects/nalogin-refactor/refactor/smsLogin.html">
            
                    
                        <b>2.9.3.4.</b>
                    
                    短信登录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9.3.5" data-path="../../projects/nalogin-refactor/refactor/login.html">
            
                <a href="../../projects/nalogin-refactor/refactor/login.html">
            
                    
                        <b>2.9.3.5.</b>
                    
                    帐密登录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.9.4" data-path="../../projects/nalogin-refactor/smarty.html">
            
                <a href="../../projects/nalogin-refactor/smarty.html">
            
                    
                        <b>2.9.4.</b>
                    
                    smarty模板
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../../projects/login-refactor/">
            
                <a href="../../projects/login-refactor/">
            
                    
                        <b>2.10.</b>
                    
                    登录服务重构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.10.1" data-path="../../projects/login-refactor/api.html">
            
                <a href="../../projects/login-refactor/api.html">
            
                    
                        <b>2.10.1.</b>
                    
                    接口设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10.2" data-path="../../projects/login-refactor/server.html">
            
                <a href="../../projects/login-refactor/server.html">
            
                    
                        <b>2.10.2.</b>
                    
                    服务端实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10.3" data-path="../../projects/login-refactor/plan.html">
            
                <a href="../../projects/login-refactor/plan.html">
            
                    
                        <b>2.10.3.</b>
                    
                    开发计划
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../../projects/portrait-url-refactor/">
            
                <a href="../../projects/portrait-url-refactor/">
            
                    
                        <b>2.11.</b>
                    
                    头像地址重构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../../projects/portrait-to-bos/">
            
                <a href="../../projects/portrait-to-bos/">
            
                    
                        <b>2.12.</b>
                    
                    头像存储迁移
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../../projects/log-masking/">
            
                <a href="../../projects/log-masking/">
            
                    
                        <b>2.13.</b>
                    
                    业务日志脱敏
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">[第三章] 服务接入</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../auth-widget/">
            
                <a href="../auth-widget/">
            
                    
                        <b>3.1.</b>
                    
                    验证控件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../auth-widget/prepare.html">
            
                <a href="../auth-widget/prepare.html">
            
                    
                        <b>3.1.1.</b>
                    
                    前期准备
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../auth-widget/token.html">
            
                <a href="../auth-widget/token.html">
            
                    
                        <b>3.1.2.</b>
                    
                    AuthToken生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../auth-widget/wap.html">
            
                <a href="../auth-widget/wap.html">
            
                    
                        <b>3.1.3.</b>
                    
                    NA/WAP端接入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../auth-widget/pc.html">
            
                <a href="../auth-widget/pc.html">
            
                    
                        <b>3.1.4.</b>
                    
                    PC端接入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../auth-widget/auth.html">
            
                <a href="../auth-widget/auth.html">
            
                    
                        <b>3.1.5.</b>
                    
                    合法性校验
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../auth-widget/code.html">
            
                <a href="../auth-widget/code.html">
            
                    
                        <b>3.1.6.</b>
                    
                    附:验证控件SDK源码
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="./">
            
                <a href="./">
            
                    
                        <b>3.2.</b>
                    
                    头像接入
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >头像接入</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5934;&#x50CF;&#x5730;&#x5740;&#x83B7;&#x53D6;"><a name="&#x5934;&#x50CF;&#x5730;&#x5740;&#x83B7;&#x53D6;" class="anchor-navigation-ex-anchor" href="#&#x5934;&#x50CF;&#x5730;&#x5740;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5934;&#x50CF;&#x5730;&#x5740;&#x83B7;&#x53D6;</h1>
<p>&#x76EE;&#x524D;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x79CD;&#x5C3A;&#x5BF8;&#x5934;&#x50CF;&#x5730;&#x5740;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x5730;&#x5740;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://himg.bdimg.com/sys/portrait/item/${sign}.jpg" target="_blank">https://himg.bdimg.com/sys/portrait/item/${sign}.jpg</a></td>
<td>&#x5C0F;&#x56FE;&#x5C3A;&#x5BF8;&#x5934;&#x50CF;</td>
</tr>
<tr>
<td><a href="https://himg.bdimg.com/sys/portraith/item/${sign}.jpg" target="_blank">https://himg.bdimg.com/sys/portraith/item/${sign}.jpg</a></td>
<td>&#x9AD8;&#x6E05;&#x5C3A;&#x5BF8;&#x5934;&#x50CF;</td>
</tr>
</tbody>
</table>
<p>&#x4EA7;&#x54C1;&#x7EBF;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#x901A;&#x8FC7;SDK&#x5305;&#x8BA1;&#x7B97;<code>sign</code>&#x8F93;&#x51FA;&#x5934;&#x50CF;&#x5730;&#x5740;&#xFF0C;&#x8C03;&#x7528;SDK&#x524D;&#x9700;&#x8981;&#x5411;Passport&#x7533;&#x8BF7;&#x4EE5;&#x4E0B;&#x4FE1;&#x606F;&#xFF1A;</p>
<ul>
<li><strong>tpl</strong>&#xFF1A;&#x4EA7;&#x54C1;&#x7EBF;&#x6807;&#x8BC6;(&#x57FA;&#x672C;&#x90FD;&#x5DF2;&#x7533;&#x8BF7;)&#x3002;</li>
<li><strong>key</strong>&#xFF1A;&#x52A0;&#x5BC6;&#x5BC6;&#x94A5;&#x3002;</li>
<li><strong>iv</strong>&#xFF1A;&#x521D;&#x59CB;&#x5316;&#x5411;&#x91CF;&#x3002;</li>
<li><strong>version</strong>&#xFF1A;&#x5BC6;&#x94A5;&#x7248;&#x672C;&#xFF0C;&#x4E0E;key&#x5BF9;&#x5E94;&#x3002;</li>
</ul>
<h3 id="&#x6CE8;&#x610F;&#x4E8B;&#x9879;"><a name="&#x6CE8;&#x610F;&#x4E8B;&#x9879;" class="anchor-navigation-ex-anchor" href="#&#x6CE8;&#x610F;&#x4E8B;&#x9879;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6CE8;&#x610F;&#x4E8B;&#x9879;</h3>
<ol>
<li><code>key</code>&#x3001;<code>iv</code>&#x4EA7;&#x54C1;&#x7EBF;&#x9700;&#x59A5;&#x5584;&#x4FDD;&#x7BA1;&#xFF0C;&#x5EFA;&#x8BAE;&#x5B58;&#x4E8E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x8BFB;&#x53D6;&#xFF0C;<code>&#x5982;&#x679C;&#x6CC4;&#x9732;&#x5C06;&#x518D;&#x6B21;&#x9020;&#x6210;&#x91CD;&#x5927;&#x5B89;&#x5168;&#x4E8B;&#x6545;</code>&#x3002;</li>
<li>&#x4E3A;&#x51CF;&#x5C11;CDN&#x7F13;&#x5B58;&#x91CF;&#xFF0C;&#x5934;&#x50CF;&#x5730;&#x5740;&#x8BF7;&#x52A1;&#x5FC5;&#x5E26;&#x4E0A;<code>.jpg</code>&#x6269;&#x5C55;&#x540D;&#xFF0C;Passport&#x8BA1;&#x5212;&#x9010;&#x6B65;&#x6536;&#x655B;&#x81F3;&#x4EC5;&#x652F;&#x6301;<code>.jpg</code>&#x8BBF;&#x95EE;&#x3002;</li>
<li>&#x8BA1;&#x7B97;&#x51FA;&#x7684;<code>sign</code>&#xFF0C;&#x4EA7;&#x54C1;&#x7EBF;&#x5E94;&#x4EC5;&#x7528;&#x4E8E;&#x5934;&#x50CF;&#x5C55;&#x793A;&#x7528;&#x9014;&#xFF0C;&#x8BF7;&#x52FF;<code>&#x89E3;&#x5BC6;</code>&#x3001;<code>&#x4F5C;&#x4E3A;&#x7528;&#x6237;&#x552F;&#x4E00;&#x6807;&#x8BC6;</code>&#x4F7F;&#x7528;&#xFF0C;&#x4EE5;&#x514D;&#x4E3A;&#x53EF;&#x80FD;&#x7684;&#x5347;&#x7EA7;&#x9020;&#x6210;&#x4E0D;&#x5FC5;&#x8981;&#x9EBB;&#x70E6;&#x3002;</li>
<li>&#x4EA7;&#x54C1;&#x7EBF;&#x63A5;&#x5165;&#x5934;&#x50CF;&#x5730;&#x5740;&#x65F6;&#xFF0C;&#x9700;&#x8FDB;&#x884C;<code>&#x5C0F;&#x6D41;&#x91CF;&#x7070;&#x5EA6;&#x4E0A;&#x7EBF;</code>&#xFF0C;&#x8054;&#x7CFB;&#x4EE5;&#x4E0B;&#x63A5;&#x53E3;&#x4EBA;&#x914D;&#x5408;&#x89C2;&#x5BDF;&#xFF0C;&#x4EE5;&#x514D;&#x5927;&#x91CF;&#x65B0;&#x5730;&#x5740;&#x7A7F;&#x900F;CDN&#x9020;&#x6210;&#x6E90;&#x7AD9;&#x5B95;&#x673A;&#x4E8B;&#x6545;&#x3002;</li>
</ol>
<h3 id="&#x63A5;&#x53E3;&#x4EBA;"><a name="&#x63A5;&#x53E3;&#x4EBA;" class="anchor-navigation-ex-anchor" href="#&#x63A5;&#x53E3;&#x4EBA;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x63A5;&#x53E3;&#x4EBA;</h3>
<ul>
<li>&#x7B2C;&#x4E00;&#x63A5;&#x53E3;&#x4EBA;&#xFF1A;&#x590F;&#x6770;(xiajie01@baidu.com)</li>
<li>&#x7B2C;&#x4E8C;&#x63A5;&#x53E3;&#x4EBA;&#xFF1A;&#x5415;&#x592A;&#x9F99;(lvtailong@baidu.com)</li>
</ul>
<h1 id="&#x5404;&#x8BED;&#x8A00;sdk"><a name="&#x5404;&#x8BED;&#x8A00;sdk" class="anchor-navigation-ex-anchor" href="#&#x5404;&#x8BED;&#x8A00;sdk"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5404;&#x8BED;&#x8A00;SDK</h1>
<p>&#x76EE;&#x524D;&#x63D0;&#x4F9B;&#x4E86;<code>PHP</code>&#x3001;<code>Golang</code>&#x3001;<code>Java</code>&#x3001;<code>C++</code>&#x56DB;&#x79CD;&#xFF0C;&#x8C03;&#x7528;&#x5404;SDK&#x7684;<code>getSign</code>&#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x83B7;&#x53D6;&#x3002;</p>
<h2 id="php"><a name="php" class="anchor-navigation-ex-anchor" href="#php"><i class="fa fa-link" aria-hidden="true"></i></a>PHP</h2>
<p>&#x6E90;&#x7801;&#xFF1A;<a href="http://icode.baidu.com/repos/baidu/odp/plugins-passport/blob/master:lib/bd/passport/Portrait.php" target="_blank">http://icode.baidu.com/repos/baidu/odp/plugins-passport/blob/master:lib/bd/passport/Portrait.php</a></p>
<p>&#x8BF7;&#x5347;&#x7EA7;ODP Passport&#x57FA;&#x7840;&#x5E93;&#x6216;&#x62F7;&#x8D1D;&#x4F7F;&#x7528;&#xFF0C;&#x82E5;&#x4F7F;&#x7528;ODP&#xFF0C;&#x53EF;&#x5728;<code>conf/passport.conf</code>&#x4E2D;&#x52A0;&#x5165;&#x7533;&#x8BF7;&#x5230;&#x7684;&#x63A5;&#x5165;&#x53C2;&#x6570;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash"><span class="token punctuation">[</span>portrait<span class="token punctuation">]</span>
tpl     <span class="token builtin class-name">:</span> xxx
version <span class="token builtin class-name">:</span> <span class="token number">1</span>
key     <span class="token builtin class-name">:</span> xxxxx
iv      <span class="token builtin class-name">:</span> xxxxx
</code></pre>
<p>&#x6216;&#x81EA;&#x884C;&#x5C06;&#x4EE5;&#x4E0A;&#x53C2;&#x6570;&#x4F20;&#x5165;<code>getSign</code>&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="golang"><a name="golang" class="anchor-navigation-ex-anchor" href="#golang"><i class="fa fa-link" aria-hidden="true"></i></a>Golang</h2>
<pre class="language-"><code class="lang-go"><span class="token keyword">package</span> portrait

<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;bytes&quot;</span>
    <span class="token string">&quot;crypto/aes&quot;</span>
    <span class="token string">&quot;crypto/cipher&quot;</span>
    <span class="token string">&quot;encoding/base64&quot;</span>
    <span class="token string">&quot;fmt&quot;</span>
    <span class="token string">&quot;hash/crc32&quot;</span>
    <span class="token string">&quot;io&quot;</span>
    <span class="token string">&quot;strconv&quot;</span>
    <span class="token string">&quot;strings&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">encryptData</span><span class="token punctuation">(</span>data <span class="token builtin">string</span><span class="token punctuation">,</span> key <span class="token builtin">string</span><span class="token punctuation">,</span> iv <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    keyBytes <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    ivBytes <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>iv<span class="token punctuation">)</span>

    result<span class="token punctuation">,</span> err <span class="token operator">:=</span> <span class="token function">aesEncrypt</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span> keyBytes<span class="token punctuation">,</span> ivBytes<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>

    data <span class="token operator">=</span> base64<span class="token punctuation">.</span>RawStdEncoding<span class="token punctuation">.</span><span class="token function">EncodeToString</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>

    data <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token string">&quot;+&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
    data <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;_&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
    data <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token string">&quot;=&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token keyword">return</span> data<span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">decryptData</span><span class="token punctuation">(</span>data <span class="token builtin">string</span><span class="token punctuation">,</span> key <span class="token builtin">string</span><span class="token punctuation">,</span> iv <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    keyBytes <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    ivBytes <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>iv<span class="token punctuation">)</span>

    <span class="token keyword">var</span> result <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span>
    <span class="token keyword">var</span> err <span class="token builtin">error</span>

    data <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;+&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
    data <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token string">&quot;_&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>

    result<span class="token punctuation">,</span> err <span class="token operator">=</span> base64<span class="token punctuation">.</span>RawStdEncoding<span class="token punctuation">.</span><span class="token function">DecodeString</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>

    origData<span class="token punctuation">,</span> err <span class="token operator">:=</span> <span class="token function">aesDecrypt</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> keyBytes<span class="token punctuation">,</span> ivBytes<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">string</span><span class="token punctuation">(</span>origData<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token boolean">nil</span>

<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">aesEncrypt</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> key <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token operator">||</span> <span class="token function">len</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">32</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;key length error&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> iv <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token operator">||</span> <span class="token function">len</span><span class="token punctuation">(</span>iv<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">16</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;iv length error&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    block<span class="token punctuation">,</span> err <span class="token operator">:=</span> aes<span class="token punctuation">.</span><span class="token function">NewCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>
    blockSize <span class="token operator">:=</span> block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    data <span class="token operator">=</span> <span class="token function">pkcs5Padding</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> blockSize<span class="token punctuation">)</span>
    blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> iv<span class="token punctuation">[</span><span class="token punctuation">:</span>blockSize<span class="token punctuation">]</span><span class="token punctuation">)</span>
    crypted <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span>
    blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>crypted<span class="token punctuation">,</span> data<span class="token punctuation">)</span>
    <span class="token keyword">return</span> crypted<span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">aesDecrypt</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> key <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token operator">||</span> <span class="token function">len</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">32</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;key length error&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> iv <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token operator">||</span> <span class="token function">len</span><span class="token punctuation">(</span>iv<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">16</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;iv length error&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    block<span class="token punctuation">,</span> err <span class="token operator">:=</span> aes<span class="token punctuation">.</span><span class="token function">NewCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>
    blockSize <span class="token operator">:=</span> block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> iv<span class="token punctuation">[</span><span class="token punctuation">:</span>blockSize<span class="token punctuation">]</span><span class="token punctuation">)</span>
    origData <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span>
    blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>origData<span class="token punctuation">,</span> data<span class="token punctuation">)</span>
    origData <span class="token operator">=</span> <span class="token function">pkcs5UnPadding</span><span class="token punctuation">(</span>origData<span class="token punctuation">)</span>
    <span class="token keyword">return</span> origData<span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">pkcs5Padding</span><span class="token punctuation">(</span>ciphertext <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> blockSize <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
    padding <span class="token operator">:=</span> blockSize <span class="token operator">-</span> <span class="token function">len</span><span class="token punctuation">(</span>ciphertext<span class="token punctuation">)</span><span class="token operator">%</span>blockSize
    padtext <span class="token operator">:=</span> bytes<span class="token punctuation">.</span><span class="token function">Repeat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span><span class="token function">byte</span><span class="token punctuation">(</span>padding<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span> padding<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token function">append</span><span class="token punctuation">(</span>ciphertext<span class="token punctuation">,</span> padtext<span class="token operator">...</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">pkcs5UnPadding</span><span class="token punctuation">(</span>origData <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
    length <span class="token operator">:=</span> <span class="token function">len</span><span class="token punctuation">(</span>origData<span class="token punctuation">)</span>
    unpadding <span class="token operator">:=</span> <span class="token function">int</span><span class="token punctuation">(</span>origData<span class="token punctuation">[</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> origData<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">(</span>length <span class="token operator">-</span> unpadding<span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">GetSign</span><span class="token punctuation">(</span>userId <span class="token builtin">int64</span><span class="token punctuation">,</span> tpl <span class="token builtin">string</span><span class="token punctuation">,</span> keyVersion <span class="token builtin">string</span><span class="token punctuation">,</span> key <span class="token builtin">string</span><span class="token punctuation">,</span> iv <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    crc32String <span class="token operator">:=</span> key <span class="token operator">+</span> iv <span class="token operator">+</span> <span class="token string">&quot;PassIdCRC32&quot;</span>
    ieee <span class="token operator">:=</span> crc32<span class="token punctuation">.</span><span class="token function">NewIEEE</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">:=</span> io<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span>ieee<span class="token punctuation">,</span> crc32String<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>
    checkSum <span class="token operator">:=</span> ieee<span class="token punctuation">.</span><span class="token function">Sum32</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    data <span class="token operator">:=</span> userId <span class="token operator">^</span> <span class="token function">int64</span><span class="token punctuation">(</span>checkSum<span class="token punctuation">)</span>

    dataString <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span> data<span class="token punctuation">)</span>
    encryptedData<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> <span class="token function">encryptData</span><span class="token punctuation">(</span>dataString<span class="token punctuation">,</span> key<span class="token punctuation">,</span> iv<span class="token punctuation">)</span>

    crc32String2 <span class="token operator">:=</span> tpl <span class="token operator">+</span> keyVersion <span class="token operator">+</span> key <span class="token operator">+</span> iv <span class="token operator">+</span> encryptedData <span class="token operator">+</span> <span class="token string">&quot;PassPortraitCheckSum&quot;</span>
    ieee<span class="token punctuation">.</span><span class="token function">Reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token boolean">_</span><span class="token punctuation">,</span> err2 <span class="token operator">:=</span> io<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span>ieee<span class="token punctuation">,</span> crc32String2<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err2 <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> err2
    <span class="token punctuation">}</span>

    sign <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%s.%s.%x.%s&quot;</span><span class="token punctuation">,</span> tpl<span class="token punctuation">,</span> keyVersion<span class="token punctuation">,</span> ieee<span class="token punctuation">.</span><span class="token function">Sum32</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> encryptedData<span class="token punctuation">)</span>
    <span class="token keyword">return</span> sign<span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">GetUserId</span><span class="token punctuation">(</span>sign <span class="token builtin">string</span><span class="token punctuation">,</span> tpl <span class="token builtin">string</span><span class="token punctuation">,</span> keyVersion <span class="token builtin">string</span><span class="token punctuation">,</span> key <span class="token builtin">string</span><span class="token punctuation">,</span> iv <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int64</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    info <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span>sign<span class="token punctuation">,</span> <span class="token string">&quot;.&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">4</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;sign format error&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    checkSumString <span class="token operator">:=</span> info<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
    data <span class="token operator">:=</span> info<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span>

    clientCheckSum<span class="token punctuation">,</span> err <span class="token operator">:=</span> strconv<span class="token punctuation">.</span><span class="token function">ParseInt</span><span class="token punctuation">(</span>checkSumString<span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;clientCheckSum %s&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    ieee <span class="token operator">:=</span> crc32<span class="token punctuation">.</span><span class="token function">NewIEEE</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token boolean">_</span><span class="token punctuation">,</span> err1 <span class="token operator">:=</span> io<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span>ieee<span class="token punctuation">,</span> tpl <span class="token operator">+</span> keyVersion <span class="token operator">+</span> key <span class="token operator">+</span> iv <span class="token operator">+</span> data <span class="token operator">+</span> <span class="token string">&quot;PassPortraitCheckSum&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err1 <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> err1
    <span class="token punctuation">}</span>

    serverCheckSum <span class="token operator">:=</span> <span class="token function">int64</span><span class="token punctuation">(</span>ieee<span class="token punctuation">.</span><span class="token function">Sum32</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    ieee<span class="token punctuation">.</span><span class="token function">Reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">if</span> clientCheckSum <span class="token operator">!=</span> serverCheckSum <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;checksum error&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    data2<span class="token punctuation">,</span> err <span class="token operator">:=</span> <span class="token function">decryptData</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> key<span class="token punctuation">,</span> iv<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> err
    <span class="token punctuation">}</span>

    data3<span class="token punctuation">,</span> err2 <span class="token operator">:=</span> strconv<span class="token punctuation">.</span><span class="token function">ParseInt</span><span class="token punctuation">(</span>data2<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err2 <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> err2
    <span class="token punctuation">}</span>

    <span class="token boolean">_</span><span class="token punctuation">,</span> err3 <span class="token operator">:=</span> io<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span>ieee<span class="token punctuation">,</span> key<span class="token operator">+</span>iv<span class="token operator">+</span><span class="token string">&quot;PassIdCRC32&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err3 <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> err3
    <span class="token punctuation">}</span>

    passId <span class="token operator">:=</span> <span class="token function">int64</span><span class="token punctuation">(</span>ieee<span class="token punctuation">.</span><span class="token function">Sum32</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">^</span> data3
    <span class="token keyword">return</span> passId<span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="java"><a name="java" class="anchor-navigation-ex-anchor" href="#java"><i class="fa fa-link" aria-hidden="true"></i></a>Java</h2>
<pre class="language-"><code class="lang-java"><span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>crypto</span><span class="token punctuation">.</span><span class="token class-name">Cipher</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>crypto<span class="token punctuation">.</span>spec</span><span class="token punctuation">.</span><span class="token class-name">IvParameterSpec</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">javax<span class="token punctuation">.</span>crypto<span class="token punctuation">.</span>spec</span><span class="token punctuation">.</span><span class="token class-name">SecretKeySpec</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span>charset</span><span class="token punctuation">.</span><span class="token class-name">StandardCharsets</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util</span><span class="token punctuation">.</span><span class="token class-name">Base64</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>zip</span><span class="token punctuation">.</span>CRC32<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PassportPortrait</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */</span>
    <span class="token keyword">protected</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">encryptData</span><span class="token punctuation">(</span><span class="token class-name">String</span> data<span class="token punctuation">,</span> <span class="token class-name">String</span> key<span class="token punctuation">,</span> <span class="token class-name">String</span> iv<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">Cipher</span> cipher <span class="token operator">=</span> <span class="token class-name">Cipher</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token string">&quot;AES/CBC/PKCS5Padding&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> raw <span class="token operator">=</span> key<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SecretKeySpec</span> secretKeySpec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SecretKeySpec</span><span class="token punctuation">(</span>raw<span class="token punctuation">,</span> <span class="token string">&quot;AES&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">IvParameterSpec</span> ivParameterSpec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">IvParameterSpec</span><span class="token punctuation">(</span>iv<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cipher<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token class-name">Cipher</span><span class="token punctuation">.</span>ENCRYPT_MODE<span class="token punctuation">,</span> secretKeySpec<span class="token punctuation">,</span> ivParameterSpec<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> encrypted <span class="token operator">=</span> cipher<span class="token punctuation">.</span><span class="token function">doFinal</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span>UTF_8<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> encodeString <span class="token operator">=</span> <span class="token class-name">Base64</span><span class="token punctuation">.</span><span class="token function">getEncoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">encodeToString</span><span class="token punctuation">(</span>encrypted<span class="token punctuation">)</span><span class="token punctuation">;</span>
        encodeString <span class="token operator">=</span> encodeString<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;+&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        encodeString <span class="token operator">=</span> encodeString<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;_&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        encodeString <span class="token operator">=</span> encodeString<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;=&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> encodeString<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */</span>
    <span class="token keyword">protected</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">decryptData</span><span class="token punctuation">(</span><span class="token class-name">String</span> data<span class="token punctuation">,</span> <span class="token class-name">String</span> key<span class="token punctuation">,</span> <span class="token class-name">String</span> iv<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        data <span class="token operator">=</span> data<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;+&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        data <span class="token operator">=</span> data<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;_&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> dataBytes <span class="token operator">=</span> <span class="token class-name">Base64</span><span class="token punctuation">.</span><span class="token function">getDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> key<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span>US_ASCII<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SecretKeySpec</span> secretKeySpec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SecretKeySpec</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;AES&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Cipher</span> cipher <span class="token operator">=</span> <span class="token class-name">Cipher</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token string">&quot;AES/CBC/PKCS5Padding&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">IvParameterSpec</span> ivParameterSpec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">IvParameterSpec</span><span class="token punctuation">(</span>iv<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cipher<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token class-name">Cipher</span><span class="token punctuation">.</span>DECRYPT_MODE<span class="token punctuation">,</span> secretKeySpec<span class="token punctuation">,</span> ivParameterSpec<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> original <span class="token operator">=</span> cipher<span class="token punctuation">.</span><span class="token function">doFinal</span><span class="token punctuation">(</span>dataBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>original<span class="token punctuation">,</span> <span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span>UTF_8<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * &#x83B7;&#x53D6;&#x5934;&#x50CF;sign
     * @param userId
     * @param tpl
     * @param version
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">getSign</span><span class="token punctuation">(</span><span class="token keyword">long</span> userId<span class="token punctuation">,</span> <span class="token class-name">String</span> tpl<span class="token punctuation">,</span> <span class="token class-name">String</span> version<span class="token punctuation">,</span> <span class="token class-name">String</span> key<span class="token punctuation">,</span> <span class="token class-name">String</span> iv<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">CRC32</span> crc32 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CRC32</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        crc32<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">(</span>key <span class="token operator">+</span> iv <span class="token operator">+</span> <span class="token string">&quot;PassIdCRC32&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> data <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>crc32<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">^</span> userId<span class="token punctuation">)</span><span class="token punctuation">;</span>
        crc32<span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        data <span class="token operator">=</span> <span class="token function">encryptData</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> key<span class="token punctuation">,</span> iv<span class="token punctuation">)</span><span class="token punctuation">;</span>

        crc32<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">(</span>tpl <span class="token operator">+</span> version <span class="token operator">+</span> key <span class="token operator">+</span> iv <span class="token operator">+</span> data <span class="token operator">+</span> <span class="token string">&quot;PassPortraitCheckSum&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> sign <span class="token operator">=</span> tpl <span class="token operator">+</span> <span class="token string">&quot;.&quot;</span> <span class="token operator">+</span> version <span class="token operator">+</span> <span class="token string">&quot;.&quot;</span> <span class="token operator">+</span> <span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token function">toHexString</span><span class="token punctuation">(</span>crc32<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;.&quot;</span> <span class="token operator">+</span> data<span class="token punctuation">;</span>
        <span class="token keyword">return</span> sign<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">long</span> <span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token class-name">String</span> sign<span class="token punctuation">,</span> <span class="token class-name">String</span> tpl<span class="token punctuation">,</span> <span class="token class-name">String</span> version<span class="token punctuation">,</span> <span class="token class-name">String</span> key<span class="token punctuation">,</span> <span class="token class-name">String</span> iv<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> info <span class="token operator">=</span> sign<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">&quot;\\.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>info<span class="token punctuation">.</span>length <span class="token operator">!=</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Exception</span><span class="token punctuation">(</span><span class="token string">&quot;token format error&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token class-name">String</span> data <span class="token operator">=</span> info<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token class-name">CRC32</span> crc32 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CRC32</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        crc32<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">(</span>tpl <span class="token operator">+</span> version <span class="token operator">+</span> key <span class="token operator">+</span> iv <span class="token operator">+</span> data <span class="token operator">+</span> <span class="token string">&quot;PassPortraitCheckSum&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">long</span> clientCheckSum <span class="token operator">=</span> <span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>info<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>clientCheckSum <span class="token operator">!=</span> crc32<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Exception</span><span class="token punctuation">(</span><span class="token string">&quot;checksum error&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        crc32<span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">long</span> dataNumber <span class="token operator">=</span> <span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token function">parseLong</span><span class="token punctuation">(</span><span class="token function">decryptData</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> key<span class="token punctuation">,</span> iv<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        crc32<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">(</span>key <span class="token operator">+</span> iv <span class="token operator">+</span> <span class="token string">&quot;PassIdCRC32&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">long</span> userId <span class="token operator">=</span> crc32<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">^</span> dataNumber<span class="token punctuation">;</span>
        <span class="token keyword">return</span> userId<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="c"><a name="c" class="anchor-navigation-ex-anchor" href="#c"><i class="fa fa-link" aria-hidden="true"></i></a>C++</h2>
<p>Makefile</p>
<pre class="language-"><code class="lang-bash"><span class="token assign-left variable">CXX</span><span class="token operator">=</span>g++
<span class="token assign-left variable">LIB</span><span class="token operator">=</span>

<span class="token assign-left variable">CXXFLAGS</span><span class="token operator">=</span> -g -std<span class="token operator">=</span>c++11 -finline-functions -Wall -lssl -lcrypto -lrt -lz -Winline -pipe -fPIC

portrait: portrait.cpp
    <span class="token variable"><span class="token variable">$(</span>CXX<span class="token variable">)</span></span> <span class="token variable"><span class="token variable">$(</span>CXXFLAGS<span class="token variable">)</span></span> $<span class="token operator">&lt;</span> -o <span class="token variable">$@</span> <span class="token variable"><span class="token variable">$(</span>INCLUDES<span class="token variable">)</span></span> <span class="token variable"><span class="token variable">$(</span>LIB<span class="token variable">)</span></span> -I<span class="token punctuation">..</span>/

clean:
    <span class="token function">rm</span> portrait
</code></pre>
<p><strong>portrait.cpp</strong></p>
<pre class="language-"><code class="lang-c++">#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>stdio.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>string</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>string.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>zlib.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>iostream</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sstream</span><span class="token punctuation">&gt;</span></span>
#include &lt;openssl/aes.h&gt;

std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
    std::string ret;
    int i = 0;
    int j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[4];
    std::string base64_chars =
            &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
            &quot;abcdefghijklmnopqrstuvwxyz&quot;
            &quot;0123456789+/&quot;;

    while (in_len--) {
        char_array_3[i++] = *(bytes_to_encode++);
        if (i == 3) {
            char_array_4[0] = (char_array_3[0] &amp; 0xfc) &gt;&gt; 2;
            char_array_4[1] = ((char_array_3[0] &amp; 0x03) &lt;&lt; 4) + ((char_array_3[1] &amp; 0xf0) &gt;&gt; 4);
            char_array_4[2] = ((char_array_3[1] &amp; 0x0f) &lt;&lt; 2) + ((char_array_3[2] &amp; 0xc0) &gt;&gt; 6);
            char_array_4[3] = char_array_3[2] &amp; 0x3f;
            for (i = 0; i &lt; 4; i++) {
                ret += base64_chars[char_array_4[i]];
            }
            i = 0;
        }
    }

    if (i) {
        for (j = i; j &lt; 3; j++) {
            char_array_3[j] = &apos;\0&apos;;
        }

        char_array_4[0] = (char_array_3[0] &amp; 0xfc) &gt;&gt; 2;
        char_array_4[1] = ((char_array_3[0] &amp; 0x03) &lt;&lt; 4) + ((char_array_3[1] &amp; 0xf0) &gt;&gt; 4);
        char_array_4[2] = ((char_array_3[1] &amp; 0x0f) &lt;&lt; 2) + ((char_array_3[2] &amp; 0xc0) &gt;&gt; 6);
        char_array_4[3] = char_array_3[2] &amp; 0x3f;

        for (j = 0; (j &lt; i + 1); j++) {
            ret += base64_chars[char_array_4[j]];
        }

        while (i++ &lt; 3) {
            ret += &apos;=&apos;;
        }
    }

    return ret;
}

std::string PKCS5Padding(std::string strParams) {
    int raw_size = strParams.size();
    int k = raw_size%AES_BLOCK_SIZE;
    int padding_size = 0;
    padding_size  = AES_BLOCK_SIZE - k;
    char* result;
    result = (char *)malloc(raw_size + padding_size);
    memcpy(result, strParams.c_str(), raw_size);
    for (int i = raw_size; i &lt; (raw_size + padding_size); i++) {
        result[i] = padding_size;
    }
    std::string res(result);
    free(result);
    return res;
}

bool aes_cbc_256_encrypt(const std::string&amp; plain_text, std::string&amp; cipher_text,
        std::string in_key, std::string in_iv) {
    AES_KEY aes;
    unsigned char iv[AES_BLOCK_SIZE] = {0};  // init vector
    unsigned char* encrypt_string = NULL;
    unsigned int len = 0;        // encrypt length (in multiple of AES_BLOCK_SIZE)
    // copy iv
    memcpy(iv, in_iv.c_str(), in_iv.length());
    // set the encryption length
    std::string data_padding = PKCS5Padding(plain_text);

    len = (plain_text.size() / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;

    cipher_text.resize(len);
    encrypt_string = reinterpret_cast<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>unsigned</span> <span class="token attr-name">char*</span><span class="token punctuation">&gt;</span></span>(&amp;cipher_text[0]);
    if (AES_set_encrypt_key((unsigned char*)in_key.c_str(), 256, &amp;aes) &lt; 0) {
        return false;
    }
    // encrypt (iv will change)
    AES_cbc_encrypt((unsigned char*)data_padding.c_str(), encrypt_string, len, &amp;aes, iv, AES_ENCRYPT);
    return true;
}

std::string encrypt_data(std::string&amp; plain_text, std::string key, std::string iv) {
    std::string aes_str;
    if (!aes_cbc_256_encrypt(plain_text, aes_str, key, iv)) {
        return &quot;&quot;;
    }

    std::string cipher_text = base64_encode((const unsigned char*)aes_str.c_str(), aes_str.size());
    // replace + / =
    std::stringstream res;
    for (std::string::size_type i = 0; i &lt; cipher_text.size(); ++i) {
        if (cipher_text[i] == &apos;+&apos;) {
            res &lt;&lt; &apos;-&apos;;
        } else if (cipher_text[i] == &apos;/&apos;) {
            res &lt;&lt; &apos;_&apos;;
        } else if (cipher_text[i] == &apos;=&apos;) {
            continue;
        } else {
            res &lt;&lt; cipher_text[i];
        }
    }
    return res.str();
}

std::string get_sign(int64_t userid, std::string tpl, std::string key_version, std::string key, std::string iv) {
    char tmp[256] = {&apos;\0&apos;};
    snprintf(tmp, sizeof(tmp), &quot;%s%s%s&quot;,
        key.c_str(), iv.c_str(), &quot;PassIdCRC32&quot;);
    uLong check_sum = crc32(0L, Z_NULL, 0);
    check_sum = crc32(check_sum, (unsigned const char*)tmp, strlen(tmp));

    int64_t data = userid^int64_t(check_sum);
    std::string data_str = std::to_string(data);
    std::string encrypt_str = encrypt_data(data_str, key, iv);
    // crc32str2
    char tmp2[256] = {&apos;\0&apos;};
    snprintf(tmp2, sizeof(tmp2), &quot;%s%s%s%s%s%s&quot;, tpl.c_str(), key_version.c_str(), key.c_str(),
            iv.c_str(), encrypt_str.c_str(), &quot;PassPortraitCheckSum&quot;);
    uLong check_sum2 = crc32(0L, Z_NULL, 0);
    check_sum2 = crc32(check_sum2, (unsigned const char*)tmp2, strlen(tmp2));
    char result[256] = {&apos;\0&apos;};
    snprintf(result, sizeof(result), &quot;%s.%s.%x.%s&quot;,
        tpl.c_str(), key_version.c_str(), (unsigned int)check_sum2, encrypt_str.c_str());
    std::string sign(result);
    return sign;
}

int main() {
    std::string sign;

    int64_t userid = 1187081601;

    sign = get_sign(userid, &quot;:tpl&quot;, &quot;:key_version&quot;, &quot;:key&quot;, &quot;:iv&quot;);
    std::cout &lt;&lt; userid &lt;&lt; &quot; &quot; &lt;&lt; sign &lt;&lt; std::endl;
    return 0;
}
</code></pre>
<h1 id="&#x5176;&#x4ED6;-&#x83B7;&#x53D6;&#x5934;&#x50CF;id"><a name="&#x5176;&#x4ED6;-&#x83B7;&#x53D6;&#x5934;&#x50CF;id" class="anchor-navigation-ex-anchor" href="#&#x5176;&#x4ED6;-&#x83B7;&#x53D6;&#x5934;&#x50CF;id"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5176;&#x4ED6;-&#x83B7;&#x53D6;&#x5934;&#x50CF;ID</h1>
<p>&#x4EA7;&#x54C1;&#x7EBF;&#x4E00;&#x822C;&#x4E0D;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x5934;&#x50CF;ID&#xFF0C;&#x4F46;&#x4EA7;&#x54C1;&#x7EBF;&#x5E38;&#x6709;&#x5B9A;&#x5236;&#x5934;&#x50CF;&#x7684;&#x9700;&#x6C42;(&#x5373;&#xFF1A;&#x5982;&#x679C;&#x4E3A;&#x9ED8;&#x8BA4;&#x5934;&#x50CF;&#x9700;&#x9ED8;&#x8BA4;&#x5C55;&#x793A;&#x4EA7;&#x54C1;&#x7EBF;&#x81EA;&#x5B9A;&#x4E49;&#x5934;&#x50CF;)&#xFF0C;&#x9488;&#x5BF9;&#x6B64;&#x9700;&#x6C42;&#x53EF;&#x4ECE; <a href="http://dev.passport.baidu.com/docs/agg/viewv2?path=tech.text&amp;doc=server/passgate/gettingstarted.text" target="_blank">passgate</a> &#x4E2D;&#x67E5;&#x8BE2;&#x5934;&#x50CF;ID(&#x5B57;&#x6BB5;&#xFF1A;<code>portraitID</code>)&#x3002;</p>
<p>&#x5386;&#x53F2;&#x539F;&#x56E0;&#xFF0C;&#x503C;&#x4E3A;&#x4EE5;&#x4E0B;&#x60C5;&#x51B5;&#x65F6;&#x4E3A;&#x9ED8;&#x8BA4;&#x5934;&#x50CF;&#xFF1A;</p>
<ul>
<li>&#x7A7A;&#x503C;null</li>
<li>&#x7A7A;&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x6570;&#x5B57;0</li>
<li>&#x6570;&#x5B57;1</li>
</ul>
<p>&#x5176;&#x4F59;&#x60C5;&#x51B5;&#x5747;&#x6709;&#x5934;&#x50CF;&#x3002;</p>
<footer class="page-footer-ex"> <span class="page-footer-ex-copyright"> &#xA9; fzu </span> &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <span class="page-footer-ex-footer-update"> &#x6700;&#x540E;&#x4FEE;&#x6539;: 2020-07-14 </span> </footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../auth-widget/code.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 附:验证控件SDK源码">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"头像接入","level":"3.2","depth":1,"previous":{"title":"附:验证控件SDK源码","level":"3.1.6","depth":2,"path":"open/auth-widget/code.md","ref":"open/auth-widget/code.md","articles":[]},"dir":"ltr"},"config":{"plugins":["multipart","codeblock-filename","anchor-navigation-ex","page-footer-ex","prism","splitter","custom-js-css","-sharing"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"prism":{"css":["prismjs/themes/prism-okaidia.css"]},"page-footer-ex":{"copyright":"© fzu","markdown":true,"update_format":"YYYY-MM-DD","update_label":"最后修改:"},"splitter":{},"search":{},"multipart":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"codeblock-filename":{},"custom-js-css":{"css":["assets/css/main.css","assets/css/folding-chapters.css"],"js":["assets/js/folding-chapters.js"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":true}},"theme":"default","author":"xiajie <xiajie01@baidu.com>","pdf":{"pageBreaksBefore":"/","headerTemplate":null,"paperSize":"a4","margin":{"right":62,"left":62,"top":36,"bottom":36},"fontSize":12,"fontFamily":"Arial","footerTemplate":null,"chapterMark":"pagebreak","pageNumbers":true},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"Passport业务手册","language":"zh-hans","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{}},"gitbook":"*","description":"Passport业务手册","extension":null},"file":{"path":"open/portrait/index.md","mtime":"2020-07-14T00:09:41.311Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-14T00:46:01.528Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-custom-js-css/fc1111ed2beb4b0151beb99a0ab82def-folding-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

